package com.guigu.lzw.controller;

import com.guigu.lzw.pojo.Usertable;
import com.guigu.lzw.service.UserTableService;
import com.guigu.lzw.service.VipService;
import com.guigu.pojo.Vip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Controller
public class Main {
    @Autowired
    VipService vs;
    @ResponseBody
    @RequestMapping("verify")
    public String verify(String phone) {
        String testUsername = "ywhang"; //在短信宝注册的用户名
        String testPassword = "13145921l"; //在短信宝注册的密码
        String testPhone = phone;
        String random = ""+Math.random();
        String substring = random.substring(random.length()-4);
        String testContent = "【硅谷医疗】您的验证码是"+substring+",５分钟内有效。若非本人操作请忽略此消息。"; // 注意测试时，也请带上公司简称或网站签名，发送正规内容短信。千万不要发送无意义的内容：例如 测一下、您好。否则可能会收不到

        String httpUrl = "http://api.smsbao.com/sms";

        StringBuffer httpArg = new StringBuffer();
        httpArg.append("u=").append(testUsername).append("&");
        httpArg.append("p=").append(md5(testPassword)).append("&");
        httpArg.append("m=").append(testPhone).append("&");
        httpArg.append("c=").append(encodeUrlString(testContent, "UTF-8"));

        String result = request(httpUrl, httpArg.toString());
        return substring;
    }
    public static String request(String httpUrl, String httpArg) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?" + httpArg;

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = reader.readLine();
            if (strRead != null) {
                sbf.append(strRead);
                while ((strRead = reader.readLine()) != null) {
                    sbf.append("\n");
                    sbf.append(strRead);
                }
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String md5(String plainText) {
        StringBuffer buf = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();
            int i;
            buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return buf.toString();
    }
    public static String encodeUrlString(String str, String charset) {
        String strret = null;
        if (str == null)
            return str;
        try {
            strret = java.net.URLEncoder.encode(str, charset);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return strret;
    }
    @ResponseBody
    @RequestMapping("seven")
    public  int seven(@RequestBody Vip vip){
        String random = ""+Math.random();
        String substring = random.substring(random.length()-4);
//        vip.setVipimg("20230213114927.jpg");
        vip.setVipid("2023"+substring);
        return vs.seven(vip);
    }
//    @RequestMapping("/Z_tx")
//    public String txUp(MultipartFile files ,@RequestBody Usertable users,HttpServletRequest req, HttpServletResponse resp) throws IOException {
//        req.setCharacterEncoding("UTF-8");
//        resp.setCharacterEncoding("UTF-8");
//        Usertable u=(Users)req.getSession().getAttribute("user");
//        Usertable login = huserService.login(u);
//        String newFileName = null;
//        String originalFilename = files.getOriginalFilename();
//        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
//        newFileName = UUID.randomUUID().toString();
//        newFileName+=substring;
//        File file = new File("D:\\images\\" + newFileName);
//        files.transferTo(file);
//        login.setPhotopath(newFileName);
//        huserService.updateByPrimaryKeySelective(login);
//        ((Users) req.getSession().getAttribute("user")).setPhotopath(newFileName);
//        return "/index.jsp";
//    }
    @ResponseBody
    @RequestMapping("login")
    public  Vip login(@RequestBody Vip vip){
        Vip vip1 = vs.queryLogin(vip);
        if(vip1!=null){
            return vip1;
        }
        Vip u = new Vip();
        u.setVippassword("0");
        return u;
    }
}
